import { _decorator, Component, EventHandler, Node, Tween, tween, UIOpacity, Vec2, Vec3 } from 'cc';
import { GameSticker, GameStickerType } from '../../Model/GameStickerMgr';
import { Unit } from '../Unit';
import { UICtrl } from '../../Ctrl/UICtrl';
const { ccclass, property } = _decorator;

@ccclass('AimTipSticker')
export class AimTipSticker extends GameSticker {
    type: GameStickerType = GameStickerType.AimTipSticker;

    public opacity: UIOpacity = null;

    setTarget(target: Unit): void {
        this.target = target;
    }

    public onClickCallback: (target: Unit) => void = null;

    public OnClick(event: EventHandler, customEventData: string) {
        this.onClickCallback && this.onClickCallback(this.target);
    }

    protected onEnable(): void {
        this.node.scale.set(10, 10, 1);
        tween(this.node).parallel(
            tween(this.node).to(0.2, {}, {
                onStart: (target) => {
                    this.opacity = this.getComponent(UIOpacity) || this.node.addComponent(UIOpacity);
                },
                onUpdate: (target, ratio) => {
                    if (this.opacity) {
                        this.opacity.opacity = ratio * 255;
                    }
                },
                easing: 'quintOut'
            }),
            tween(this.node).by(0.5, { angle: 180 }),
            tween(this.node).to(0.5, { scale: new Vec3(1, 1, 1) }, { easing: 'cubicInOut' })
        ).start();
    }

    protected onDisable(): void {
        Tween.stopAllByTarget(this.node);
        this.node.scale.set(10, 10, 1);
    }

    protected update(dt: number) {
        if (!this.target || !this.target.isValid) {
            this.target = null;
        }
        else {
            if (this.target.isInScreenPos) {
                this.node.setWorldPosition(this.target.unitWorldPos);
            }
            else {
                UICtrl.ClearStickerRenderer(this.target, this.type);
            }
        }
    }
}

